package com.hawk.eye.server.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.server.domain.dto.req.RepairApiLogReqDTO;
import com.hawk.eye.server.domain.dto.resp.RepairApiLogRespDTO;
import com.hawk.eye.server.domain.po.RepairApiLog;
import com.hawk.eye.server.mapper.RepairApiLogMapper;
import com.hawk.eye.server.service.IRepairApiLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @className: RepairApiLogServiceImpl
 * @description: TODO 类描述
 * @author: salad
 * @date: 2024/6/2
 **/
@Service
@RequiredArgsConstructor
public class RepairApiLogServiceImpl extends ServiceImpl<RepairApiLogMapper, RepairApiLog> implements IRepairApiLogService {


    private final RepairApiLogMapper repairApiLogMapper;

    @Override
    public void saveExecLog(RepairApiLogReqDTO reqDTO) {
        RepairApiLog ral = new RepairApiLog();
        ral.setExecCost(reqDTO.getCost());
        ral.setExecSuccessful(reqDTO.getExecSuccessful());
        ral.setExecResult(reqDTO.getResult());
        ral.setExecTime(reqDTO.getExecTime());
        ral.setRepairApiId(reqDTO.getRepairApiId());
        ral.setExecUser(reqDTO.getExecUser());
        ral.setReqBody(reqDTO.getReqBody());
        ral.setReqType(reqDTO.getReqType());
        ral.setReqUri(reqDTO.getReqUri());
        ral.setReqParams(reqDTO.getReqParams());
        ral.setReqHeaders(reqDTO.getReqHeaders());
        save(ral);
    }

    @Override
    public PageResult<RepairApiLogRespDTO> page(IPage<RepairApiLog> page, Long repairId) {
        IPage<RepairApiLogRespDTO> pages = repairApiLogMapper.page(page, repairId);
        for (RepairApiLogRespDTO record : pages.getRecords()) {
            if (record.getExecResult() != null) {
                record.setExecResult(record.getExecResult().replace("\\r\\n\\t", System.lineSeparator()));
            }
        }
        return PageResult.of(pages);
    }

}
